<template>
  <el-container>
    <el-header>
      <el-row class="visitandpiont">
        <el-col>
          <h1 class="carout">车辆白名单</h1>
        </el-col>
      </el-row>
    </el-header>
    <el-main>
      <div class="today" v-show="tab === 1">
        <el-row style="text-align: end">
          <el-button @click="dialogFormVisible = true" class="buttonb">
            新建
          </el-button>
        </el-row>

        <el-row class="underpiontment">
          <el-col class="way">
            <h6 style="margin-top: 20px">状态</h6>
          </el-col>
          <el-col class="drawway">
            <el-select v-model="formInline.region">
              <el-option label="出" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-col>
          <el-col class="pointime">
            <h6 style="margin-top: 20px">时间</h6>
          </el-col>
          <el-col class="redrawwayblock">
            <div class="block">
              <el-date-picker
                v-model="value1"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
          </el-col>
          <el-col class="input">
            <el-input
              v-model="formInline.user"
              placeholder="输入关键字"
            ></el-input>
          </el-col>
          <el-col class="find">
            <el-button type="primary" @click="findBy()">查询</el-button>
          </el-col>
        </el-row>

        <el-row class="table">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="id" label="编号">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="licensePlateNumber"
              label="车牌号"
            ></el-table-column>
            <el-table-column prop="ower" label="车主"></el-table-column>
            <el-table-column prop="telephone" label="手机号"></el-table-column>
            <el-table-column prop="createTime" label="时间"></el-table-column>
            <el-table-column width="260" label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="
                    handleEdit(scope.$index, scope.row),
                      (dialogFormVisible1 = true)
                  "
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
                <el-dialog :visible.sync="dialogFormVisible">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="车主" :label-width="formLabelWidth">
                      <el-input
                        v-model="form.name"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="车牌号" :label-width="formLabelWidth">
                      <el-input
                        clearable
                        v-model="form.name1"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>

                    <el-form-item label="电话号" :label-width="formLabelWidth">
                      <el-input
                        v-model="form.name2"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button
                      class="dialog-footer1"
                      @click="dialogFormVisible = false"
                      >取 消</el-button
                    >
                    <el-button
                      class="dialog-footer2"
                      type="primary"
                      @click="add"
                      >确 定</el-button
                    >
                  </div>
                </el-dialog>
                <el-dialog :visible.sync="dialogFormVisible1">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="车主" :label-width="formLabelWidth">
                      <el-input
                        v-model="form.name3"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="车牌号" :label-width="formLabelWidth">
                      <el-input
                        clearable
                        v-model="form.name4"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>

                    <el-form-item label="电话号" :label-width="formLabelWidth">
                      <el-input
                        v-model="form.name5"
                        autocomplete="off"
                      ></el-input>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button
                      class="dialog-footer1"
                      @click="dialogFormVisible1 = false"
                      >取 消</el-button
                    >
                    <el-button
                      class="dialog-footer2"
                      type="primary"
                      @click="update_car"
                      >确 定</el-button
                    >
                  </div>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      formInline: {
        user: "",
        region: "",
      },
      value: "", //日期选择
      value1: "",

      tableData: [
        {
          id: 1,
          licensePlateNumber: 123,
          ower: 1,
          telephone: 1,
          createTime: 1,
        },
      ],
      tab: 1,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        name: "",
        name1: "",
        name2: "",
        name3: "",
        name4: "",
        name5: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "80px",
    }
  },
  computed:{
    scId(){
      return this.$store.state.scId
    }
  },

  methods: {
    getdate(date) {
      let now = date,
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate()
      return (
        y +
        "-" +
        (m < 10 ? "0" + m : m) +
        "-" +
        (d < 10 ? "0" + d : d) +
        " " +
        now.toTimeString().substr(0, 8)
      )
    },
     getCarWhileListFindAll() {
      console.log(this.scId)
        this.$axios({
        methods: "get",
        url: `api/${this.scId}/CarWhileList/findAll`,
      })
        .then((res) => {
          let data = res.data.data
          this.tableData = data
          for (let i = 0; i < this.tableData.length; i++) {
            this.tableData[i].createTime = this.getdate(
              new Date(data[i].createTime)
            )
          }
          console.log(data)
        })
        .catch((err) => {
          this.$message.error("请求失败")
          console.log(err)
        })
    },
    async findBy() {
      if (this.value1 == null) {
        console.log("null")
      } else {
        this.$axios({
          url: `/api/${this.scId}/CarWhileList/findByLicenseAndDate`,
          method: "post",
          data: {
            licensePlateNumber: this.formInline.user,
            start: this.value1[0],
            end: this.value1[1],
          },
          headers: {
            "Content-Type": "application/json",
          },
        }).then((res) => {
          let data = res.data.data
          this.tableData = data
          for (let i = 0; i < this.tableData.length; i++) {
            this.tableData[i].time = this.getdate(new Date(data[i].time))
          }
        })
      }
    },
    async add() {
      this.$axios({
        url: `/api/${this.scId}/CarWhileList/add`,
        method: "post",
        data: {
          ower: this.form.name,
          licensePlateNumber: this.form.name1,
          telephone: this.form.name2,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res)
        this.getCarWhileListFindAll()
      })
      this.dialogFormVisible = false
    },
    async delete_car(id) {
      this.$axios({
        url: `/api/${this.scId}/CarWhileList/delete`,
        method: "post",
        data: {
          id: id,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res)
        console.log(this.tableData.splice())
        this.getCarWhileListFindAll()
      })
    },
    async update_car() {
      this.$axios({
        url: `/api/${this.scId}/CarWhileList/update`,
        method: "post",
        data: {
          id: this.form.id,
          ower: this.form.name3,
          licensePlateNumber: this.form.name4,
          telephone: this.form.name5,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res)
        this.getCarWhileListFindAll()
      })
      this.dialogFormVisible1 = false
    },
    onSubmit() {
      console.log("submit!")
    },
    handleEdit(index, row) {
      console.log(index, row)
      this.form.id = row.id
      this.form.name3 = row.ower
      this.form.name4 = row.licensePlateNumber
      this.form.name5 = row.telephone
      // console.log(this.form.id)
      // this.update_car(row)
    },
    handleDelete(index, row) {
      console.log(index, row)
      this.delete_car(row.id)
    },
  },
  components: {},
  mounted() {
    this.getCarWhileListFindAll()
  },
  watch: {
    scId: {
      handler(){
        this.getCarWhileListFindAll()
      },
      immediate: true,
      deep: true
    }
  }
}
</script>

<style scoped>
.el-header {
  background-color: white;
  width: 1000px;
  margin: 0px 20px;
  padding: 0;
}
.visitandpiont {
  display: flex;
  margin-left: 0%;
}
.piontment {
  width: 350px;
  height: 69px;
  padding: 20px;
  border: solid 1px;
}
.underpiontment {
  width: 1400px;
  height: 60px;
  margin: 20px 0px;
  display: flex;
}
.dialog-body {
  width: 600px;
  height: 300px;
  margin-left: 100px;
  margin-top: 100px;
  margin-right: 100px;
}
.dialog-footer1 {
  position: absolute;
  left: 30%;
  bottom: 20%;
}
.dialog-footer2 {
  position: absolute;
  bottom: 20%;
  right: 30%;
}
.way {
  width: 100px;

  margin-left: -30px;
}
.drawway {
  width: 300px;
  padding: 10px 15px;
  margin-left: -30px;
}
.buttonb {
  height: 40px;
  width: 80px;
  padding: 0px 20px;
}
.redrawway {
  width: 300px;
  margin: 0px 10px;
  padding: 10px 0px;
}
.state {
  width: 100px;

  padding: 20px 0px;
}
.input {
  width: 300px;
  padding: 10px 0px;
  margin-left: 160px;
}
.find {
  width: 70px;
  height: 70px;
  margin: 0px 60px;
  padding: 10px 0px;
}
.carout {
  height: 63px;
  width: 292px;
  padding: 20px;
  margin-left: 10px;
  display: flex;
}

.pointime {
  width: 300px;

  margin-left: -100px;
}
.drawwayblock {
  width: 280px;
  margin: 0px 30px;
  padding: 10px 15px;
}
.piontmenttime {
  width: 90px;
  padding: 20px 0px;
}
.redrawwayblock {
  width: 280px;
  margin-left: -100px;
  padding: 10px 15px;
}

.el-main {
  background-color: white;
  text-align: center;
  margin: 0px 20px;
}
</style>
